package com.java.comprehensive.arithmetic.leecode.tags.array;

import java.util.Arrays;

/**
 * @Author: 窦绍飞
 * @Date: 2022/6/28
 */
public class LeetCode_34 {


	public static int[] searchRange(int[] nums, int target) {
		int[] result = {-1, -1};
		if (nums.length < 1) return result;

		int l = 0, r = nums.length - 1;
		while (l < r) {
			int mid = (l + r) / 2;
			if (nums[mid] < target) {
				l = mid + 1;
			} else if (nums[mid] >= target) {
				r = mid;
			}
		}
		if (nums[l] != target) {
			return result;
		}
		int ll = 0, rr = nums.length - 1;
		while (ll < rr) {
			int mid = (ll + rr + 1) / 2;
			if (nums[mid] <= target) {
				ll = mid;
			} else if (nums[mid] > target) {
				rr = mid - 1;
			}
		}
		if (nums[ll] != target) {
			return result;
		}
		result[0] = l;
		result[1] = ll;
		return result;
	}


	public static void main(String[] args) {
		int[] nums = {5, 7, 7, 8, 8, 10};
		System.out.println(Arrays.toString(searchRange(nums, 8)));
	}
}
